package cn.hxsteel.purview.module.purview;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hxsteel.model.bo.ViewBO;
import cn.hxsteel.purview.core.base.BaseController;
import cn.hxsteel.purview.core.config.constant.ActionConst;
import cn.hxsteel.purview.core.config.constant.RoleConst;
import cn.hxsteel.purview.core.exception.PermitFailException;
import cn.hxsteel.purview.core.tool.PurviewTool;
import cn.hxsteel.purview.model.bo.ModuleBO;
import cn.hxsteel.purview.model.po.RolePO;
import cn.hxsteel.purview.model.vo.SessionVO;

/**
 * 权限模块的控制器
 */
@Controller
public class PurviewController extends BaseController
{
    @Autowired
    private PurviewService _purviewService;
    
    /* 查询
    --------------------------------------------------*/
    
    /**
     * 权限的主视图
     */
    @RequestMapping( "/purviewManage/view" )
    public ModelAndView purviewManageView( HttpServletRequest request )
    {
	ModelAndView res = new ModelAndView();
	String viewName = getRedirectUrl( "/purviewRole/view" );
	res.setViewName( viewName );
	return res;
    }
    
    /**
     * 角色的主视图
     */
    @RequestMapping( "/purviewRole/view" )
    public ModelAndView roleView( HttpServletRequest request )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/purview/role/list" );
	res.addObject( ACTION_SET_KEY, getUserModuleActionNameSet( request ) );
	return res;
    }
    
    /**
     * 角色的持久对象的列表数据
     */
    @RequestMapping( "/purviewRole/view/do" )
    @ResponseBody
    public List< RolePO > rolePOListData( HttpServletRequest request )
    {
	List< RolePO > res = null;
	int uid = getSessionVO( request ).getUid();
	if ( uid > 0 )
	{
	    res = _purviewService.selUserRolePOList( uid );
	}
	return res;
    }
    
    /* 角色添加
    --------------------------------------------------*/
    
    /**
     * 添加角色的视图
     */
    @RequestMapping( "/purviewRole/add" )
    public ModelAndView addRoleView( HttpServletRequest request )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/purview/role/add" );
	int uid = getSessionVO( request ).getUid();
	res.addObject( "uid", uid );
	return res;
    }
    
    /**
     * 添加角色的持久对象
     * 
     * @param rolePO 角色的持久对象
     */
    @RequestMapping( "/purviewRole/add/do" )
    public ModelAndView addRolePO( RolePO rolePO )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/purview/role/add" );
	res.addObject( "uid", rolePO.getUid() );
	_purviewService.addRolePO( rolePO );
	return res;
    }
    
    /* 角色修改
    --------------------------------------------------*/
    
    /**
     * 修改角色的视图
     * 
     * @param roleID 角色标识
     */
    @RequestMapping( "/purviewRole/edit" )
    public ModelAndView updRoleView( Short roleID, HttpServletRequest request )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/purview/role/edit" );
	roleID = roleID == null ? 0 : roleID;
	RolePO rolePO = _purviewService.selRolePO( roleID );
	int uid = getSessionVO( request ).getUid();
	if ( uid != rolePO.getUid() ) { throw new PermitFailException(); }
	res.addObject( "role", rolePO );
	return res;
    }
    
    /**
     * 修改角色的持久对象
     * 
     * @param rolePO 角色对象
     */
    @RequestMapping( "/purviewRole/edit/do" )
    public ModelAndView updRolePO( RolePO rolePO )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/purview/role/list" );
	_purviewService.updRolePO( rolePO );
	return res;
    }
    
    /**
     * 设置角色权限的视图
     * 
     * @param roleID 角色标识
     */
    @RequestMapping( "/purviewRole/set" )
    public ModelAndView setRolePermitView( Short roleID, HttpServletRequest request )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/purview/role/permit" );
	roleID = roleID == null ? 0 : roleID;
	RolePO rolePO = _purviewService.selRolePO( roleID );
	int uid = -1;
	SessionVO< ? > sessionVO = getSessionVO( request );
	if ( sessionVO.getRoleIDSet().contains( RoleConst.SYSTEM_SIGN ) )
	{
	    uid = 0;
	}
	else
	{
	    uid = getSessionVO( request ).getUid();
	}
	if ( uid == -1 || ( uid > 0 && uid != rolePO.getUid() ) ) { throw new PermitFailException(); }
	res.addObject( "roleID", rolePO.getId() );
	res.addObject( "viewActionIDSign", ActionConst.VIEW_ACTIONID_SIGN );
	res.addObject( "userModuleList", _purviewService.getUserModuleBOList( uid ) );
	res.addObject( "roleModuleIDSet", _purviewService.getRoleModuleIDSet( rolePO.getId() ) );
	res.addObject( "userActionVOListMap", _purviewService.getUserActionVOListMap( uid ) );
	res.addObject( "roleActionIDSet", _purviewService.getRoleActionIDSet( rolePO.getId() ) );
	return res;
    }
    
    /**
     * 设置角色权限的数据
     * 
     * @param roleID 角色标识
     * @param actionIDGroup 动作标识组
     */
    @RequestMapping( "/purviewRole/set/do" )
    public ModelAndView setRolePermitData( Short roleID, String actionIDGroup )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/purview/role/list" );
	_purviewService.setRolePermitData( roleID, actionIDGroup );
	return res;
    }
    
    /* 角色删除
    --------------------------------------------------*/
    
    /**
     * 删除角色的视图
     * 
     * @param roleID 角色标识
     */
    @RequestMapping( "/purviewRole/delete" )
    public ModelAndView delRoleView( Short roleID )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/purview/role/delete" );
	roleID = roleID == null ? 0 : roleID;
	res.addObject( "role", _purviewService.selRolePO( roleID ) );
	return res;
    }
    
    /**
     * 删除角色的持久对象
     * 
     * @param roleID 角色标识
     */
    @RequestMapping( "/purviewRole/delete/do" )
    public ModelAndView delRolePO( Short roleID )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/purview/role/delete" );
	return res;
    }
    
    /* 公共部分
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    
    /**
     * 侧边导航的视图
     */
    @RequestMapping( "/page/nav/side" )
    public ModelAndView navSide( HttpServletRequest request )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/purview/sideNav" );
	List< ModuleBO > navList = PurviewTool.getUserParentModuleBOList( request );
	res.addObject( "navList", navList );
	return res;
    }
    
    /**
     * 顶部导航的视图
     */
    @RequestMapping( "/page/nav/top" )
    public ModelAndView navTop( HttpServletRequest request )
    {
	ModelAndView res = new ModelAndView();
	res.setViewName( "/purview/topNav" );
	List< ModuleBO > navList = PurviewTool.getUserChildModuleBOList( request );
	res.addObject( "navList", navList );
	return res;
    }
    
    /**
     * 上下级导航地址的数据
     * 
     * @param uri 相对的资源地址
     */
    @RequestMapping( "/page/nav/do" )
    @ResponseBody
    public ViewBO navData( String uri, HttpServletRequest request )
    {
	return getUserModuleURIMap( uri, request );
    }
}